
class Signal:
    def __init__(self,name,msb,dlc,factor,offset,minValue,maxValue,unit = ''):
        self.name = name     #信号名
        self.msb  = int(msb)
        self.dlc = int(dlc)  #数据长度
        self.factor = self.__isInt(factor)
        self.offset = self.__isInt(offset)
        self.minValue = self.__isInt(minValue)
        self.maxValue = self.__isInt(maxValue)
        self.unit = unit
        self.UnitDict = {'degC': 10,'℃': 10,'v':10,'V':10,'A':10,'Mpa':1000,'MPa':1000,'km_per_h':100,'rpm':1,'percent':1,'%':1,'kmph':100,'K':10}
        self.signalStr_lenLimt = 40
        self.valueNeedConvent_Flag = False
        self.valueType = 'uint8'
        self.__Get_signalMoreInfo()
    def __Get_signalMoreInfo(self):
        self.unitRate = self.UnitDict.get(self.unit,1)
        if ('v' == self.unit) or ('V' == self.unit) and (self.maxValue < 26):
            self.unitRate = 1000
        #判断数据在RTE层中所需要的位数长度
        if self.minValue >= 0:
            if self.maxValue * self.unitRate > 65535:
                self.varLen = 32
                self.structEle_str = 'e_dw_'
                self.varType = ' '*4 + 'uint32 '
                self.valueType = 'uint32'
            elif self.maxValue * self.unitRate > 255:
                self.varLen = 16
                self.structEle_str = 'e_w_'
                self.varType = ' '*4 + 'uint16 '
                self.valueType = 'uint16'
            else:
                self.varLen = self.dlc
                if self.maxValue > 1:
                    self.structEle_str = 'e_u_'
                    self.varType = ' '*4 + 'uint8  '
                    self.valueType = 'uint8'

                else:
                    self.structEle_str = 'e_b_'
                    self.varType = ' '*4 +'bool   '
                    self.valueType = 'bool'

        else:
            self.varType = ' '*4 + 'sint16 '
            self.varLen = 16
            self.structEle_str = 'e_sw_'
        #判断是否RTE数据与报文原始数据之间是否存在转换关系
        if self.factor != 1 or self.offset != 0 or self.unitRate != 1:
            self.valueNeedConvent_Flag = True
        if self.dlc <= 8:
            self.rawType = 'uint8'
        elif self.dlc <= 16:
            self.rawType = 'uint16'
        else:
            self.rawType = 'uint32'
        if ((self.msb % 8) + 1) < self.dlc:
            self.changFlag = True
        else:
            self.changFlag = False
        # self.signalTypedef_str = (self.varType + self.structEle_str +self.name).ljust(self.signalStr_lenLimt) + ': ' + str(self.varLen).ljust(2) +';'

    def __isInt(self,value):
        if len(value.split('.')) <=1:
            return int(value)
        else:
            return float(value)

